{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "课程演示死锁, 但是本地代码没死锁..., 非必现问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "cpp"
    }
   },
   "outputs": [],
   "source": [
    "// main所在主线程\n",
    "\n",
    "ThreadPool::~ThreadPool() {\n",
    "  this->isPoolRunning_ = false;\n",
    "  notEmpty_.notify_all(); // 通知ThreadPool::threadFunc中的\n",
    "\n",
    "  std::unique_lock<std::mutex> lock(this->taskQueMtx_);\n",
    "  exitCond_.wait(lock, [&](){return this->threads_.size() == 0;}); // 1.阻塞, 2. 正在执行, 3. 卡在isPoolRunning_与获取锁之间\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "cpp"
    }
   },
   "outputs": [],
   "source": [
    "// 线程池的线程函数\n",
    "\n",
    "void ThreadPool::threadFunc(int threadID) {\n",
    "  // ...\n",
    "\n",
    "  // 在运行isPoolRunning_\n",
    "  // 队列为空taskQue_.empty()\n",
    "  // 等待锁notEmpty_\n",
    "  // 执行task->exec()\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![condition](./images/condition.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "name": "C++17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
